#include <iostream>
#include <cstdio>
#include<cstring>
#include<cstdlib>
#include<cstdbool>

#define COMMAND_SIZE 1024 
#define FORMAT "[%s@%s %s]# "

#define MAXARGC 128
char*  g_argv[MAXARGC];
int g_argc = 0;
const char*  GetUser()
{
  const char* username = getenv("USER");
  return username == NULL?"None":username;
}

const char* GetHostname()
{
  const char* hostname  = getenv("HOSTNAME");
  return hostname == NULL?"None":hostname;
}

const char* GetPwd()
{
  const char* pwd = getenv("PWD");
  return pwd == NULL?"None":pwd;
}

void makecommandline(char cline[], int size)
{
  snprintf(cline,size,FORMAT,GetUser(),GetHostname(),GetPwd());
}

void printfcommandline()
{
  char cline[COMMAND_SIZE];
  makecommandline(cline,sizeof(cline));
  printf("%s",cline);
  fflush(stdout);
}

bool Getcommandline(char* commandline, int size)
{
  char* r = fgets(commandline,size,stdin);
  if(r == NULL) return false;
  commandline[strlen(commandline)-1] = 0;
  if(strlen(commandline) == 0) return false;
  return true;

}

bool commandanslyse(char* commandline)
{
    g_argc = 0;
    g_argv[g_argc++] = strtok(commandline," ");
    while((bool)(g_argv[g_argc++] = strtok(nullptr," ")));
    g_argc--;
    return true;
}

void printfargv()
{
  int i = 0;
  printf("%d\n",g_argc);
  for(i = 0; g_argv[i]; i++)
  {
    printf("g_argv[%d]->%s\n",i,g_argv[i]);
  }
}

int main()
{

  while(1)
  {

    //printf("[%s@%s %s]#",GetUser(),GetHostname(),GetPwd());
    printfcommandline();
    //获取数据
    char Commandline[COMMAND_SIZE];
    if(Getcommandline(Commandline,sizeof(Commandline)))
    {
       printf("%s\n",Commandline);

    }
    else
    {
      continue;
    }
    //此时一定已经获取数据
    commandanslyse(Commandline);
    printfargv();
    }
    return 0;
}
